
const { ccclass, property } = cc._decorator;

@ccclass
export default class PageView extends cc.PageView {

    onEnable() {
        super.onEnable();
        //this.noVertical = true;
    }
    onDisable() {
        super.onDisable();
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchEnd, this);
    }
    _scrollChildren(deltaMove) {
        let t:any = this;
        deltaMove = t._clampDelta(deltaMove);

        let realMove = deltaMove;
        //纵向移动大于横向移动时取消横向移动
        if (realMove.y/realMove.x > 1 ||realMove.y/realMove.x < -1) {
            realMove.x = 0;
        }
        //
        let outOfBoundary;
        if (this.elastic) {
            outOfBoundary = t._getHowMuchOutOfBoundary();
            realMove.x *= (outOfBoundary.x === 0 ? 1 : 0.5);
            realMove.y *= (outOfBoundary.y === 0 ? 1 : 0.5);
        }

        if (!this.elastic) {
            outOfBoundary = t._getHowMuchOutOfBoundary(realMove);
            realMove = realMove.add(outOfBoundary);
        }

        let scrollEventType:any = -1;

        if (realMove.y > 0) { //up
            let icBottomPos = this.content.y - this.content.anchorY * this.content.height;

            if (icBottomPos + realMove.y >= t._bottomBoundary) {
                scrollEventType = 'scroll-to-bottom';
            }
        }
        else if (realMove.y < 0) { //down
            let icTopPos = this.content.y - this.content.anchorY * this.content.height + this.content.height;

            if (icTopPos + realMove.y <= t._topBoundary) {
                scrollEventType = 'scroll-to-top';
            }
        }
        if (realMove.x < 0) { //left
            let icRightPos = this.content.x - this.content.anchorX * this.content.width + this.content.width;
            if (icRightPos + realMove.x <= t._rightBoundary) {
                scrollEventType = 'scroll-to-right';
            }
        }
        else if (realMove.x > 0) { //right
            let icLeftPos = this.content.x - this.content.anchorX * this.content.width;
            if (icLeftPos + realMove.x >= t._leftBoundary) {
                scrollEventType = 'scroll-to-left';
            }
        }

        t._moveContent(realMove, false);

        if (realMove.x !== 0 || realMove.y !== 0) {
            if (!t._scrolling) {
                t._scrolling = true;
                t._dispatchEvent('scroll-began');
            }
            t._dispatchEvent('scrolling');
        }

        if (scrollEventType !== -1) {
            t._dispatchEvent(scrollEventType);
        }

    }
    onTouchMove(event: cc.Event.EventTouch) {
        if (event.getDeltaY() > 2 || event.getDeltaY() < -2)
            event.stopPropagationImmediate();

    }
    onTouchEnd() {

    }
    _hasNestedViewGroup(event, captureListeners) {
        if (event.eventPhase !== cc.Event.CAPTURING_PHASE) return;
        if (!event.touch) return;

        const moveDelta = event.touch.getDelta();
        // TODO： 2.如果是上移移动则直接吞噬
        if (captureListeners) {
            for (let i = 0; i < captureListeners.length; ++i) {
                const item = captureListeners[i];
                if (item) {
                    // 自身节点
                    if (this.node === item) {
                        if (moveDelta.y === 0) {
                            return false;
                        }
                        //屏蔽掉此判断，会导致scrollview没有回弹效果
                        if (event.target.getComponent(cc.ViewGroup)) {
                        return true;
                        }
                    }
                    // 其他节点但有viewGroup
                    if (item.getComponent(cc.ViewGroup)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
